﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project.Admin
{
    public partial class EditRoute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            DataPOFDataContext db = new DataPOFDataContext();
            string acc = (from x in db.AccountLogins where x.Username == Session["username"].ToString() select x.FullName).FirstOrDefault();
            Label1.Text = acc;
            int ac = (from x in db.Locations select x.Location1).Count();
            Label3.Text = Convert.ToString(ac + 1);
            int routedetailID = (from x in db.RouteDetails select x.ID).Count();
            Label6.Text = Convert.ToString(routedetailID + 1);
            Label5.Text = (from x in db.Locations where x.ID == ac select x.Location1).FirstOrDefault();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = null;
            Response.Cache.SetNoStore();
            Response.CacheControl = "no-cache";
            Response.Redirect("../Home/Login.aspx");
        }
        
        
        protected void Button2_Click1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            int a = (from x in db.Locations where x.Location1 == TextBox2.Text select x.ID).FirstOrDefault();
            if (TextBox2.Text == "") { Label4.Text = "Please enter the Location!"; }
            else if (a == 0)
            {

                Location Location = new Location();
                Location.ID = Convert.ToInt32(Label3.Text);
                Location.Location1 = TextBox2.Text;

                try
                {

                    db.Locations.InsertOnSubmit(Location);

                }

                catch (Exception ex)
                {

                    //MessageBox.Show(ex.Message);

                }

                finally
                {

                    // Submit to data

                    db.SubmitChanges();
                    Response.Redirect("../Admin/EditRoute.aspx");

                }
            }
            else Label4.Text = " Location already!";
        }

        protected void Button3_Click1(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            int IdFrom = (from x in db.Locations where x.Location1 == Label5.Text select x.ID).FirstOrDefault();
            int IdTo = (from x in db.Locations where x.Location1 == DropDownList1.SelectedItem.ToString() select x.ID).FirstOrDefault();
            string route = Label5.Text + DropDownList1.SelectedItem.ToString();
            int routeID = (from x in db.Routes select x.ID).Count();

            if (TextBox3.Text == "") { Label7.Text = "Please enter the Distance!"; }
            else if (Convert.ToInt32(TextBox3.Text) <= 0) { Label7.Text = "Please enter the Distance > 0 !"; } else
                
                
            {
                // Add Route
                Route Route = new Route();
                Route.ID = routeID + 1;
                Route.Route1 = route;

                try
                {

                    db.Routes.InsertOnSubmit(Route);

                }

                catch (Exception ex)
                {

                    //MessageBox.Show(ex.Message);

                }

                finally
                {
                    // Submit to data
                    db.SubmitChanges();
                }

                //add RouteDetail


                RouteDetail RouteDetail = new RouteDetail();
                RouteDetail.ID = (from x in db.RouteDetails select x.ID).Count() + 1;
                RouteDetail.RouteID = (from x in db.Routes where x.Route1 == route select x.ID).FirstOrDefault();
                RouteDetail.From = IdFrom;
                RouteDetail.To = IdTo;
                RouteDetail.Distance = Convert.ToInt32(TextBox3.Text);

                try
                {

                    db.RouteDetails.InsertOnSubmit(RouteDetail);

                }

                catch (Exception ex)
                {

                    //MessageBox.Show(ex.Message);

                }

                finally
                {

                    // Submit to data

                    db.SubmitChanges();
                    Response.Redirect("../Admin/EditRoute.aspx");

                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Label5.Text == DropDownList1.SelectedItem.ToString()) { TextBox3.Text = "0"; TextBox3.Enabled = false; }
        }

    
    }
}